package com.lonely.order.support.synchronizer;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * @author : 黄志标
 * @date: 2025/8/21 10:50
 * @description:
 */
public class SynchronizedOrderDeliverThreadLocal {


    /**
     * 创建人
     */
    public static ThreadLocal<Long> CREATE_BY = ThreadLocal.withInitial(() -> 1L);

    /**
     * 是否是websocket访问
     */
    private static final ThreadLocal<Boolean> IS_WEBSOCKET = ThreadLocal.withInitial(() -> false);

    /**
     * 客户端监听队列名称
     */
    private static final ThreadLocal<String> DESTINATION = new ThreadLocal<>();

    /**
     * 请求id
     */
    private static final ThreadLocal<String> REQUEST_ID = new ThreadLocal<>();

    /**
     * 当前处理第n条数据
     */
    private static final ThreadLocal<Integer> CURRENT_SIZE = ThreadLocal.withInitial(() -> 0);

    /**
     * 总条数
     */
    private static final ThreadLocal<Integer> TOTAL_SIZE = ThreadLocal.withInitial(() -> 0);

    public static void setIsWebsocket(Boolean isWebsocket) {
        IS_WEBSOCKET.set(isWebsocket);
    }

    public static Boolean getIsWebsocket() {
        return IS_WEBSOCKET.get();
    }

    /**
     * 获取创建人
     */
    public static Long getCreateBy() {
        return CREATE_BY.get();
    }

    /**
     * 是否人为触发
     *
     * @return
     */
    public static void setCreateBy(Long createBy) {
        CREATE_BY.set(createBy);
    }



    /**
     * 设置已处理数
     *
     * @param currentSize
     */
    public static void setCurrentSize(Integer currentSize) {
        CURRENT_SIZE.set(currentSize);
    }

    /**
     * 添加已处理数
     *
     * @param addSize
     */
    public static void addExecutedSize(Integer addSize) {
        CURRENT_SIZE.set(CURRENT_SIZE.get() + addSize);
    }

    /**
     * 设置请求id
     * @param requestId
     */
    public static void setRequestId(String requestId) {
        REQUEST_ID.set(requestId);
    }

    /**
     * 获取请求id
     * @return
     */
    public static String getRequestId() {
        return REQUEST_ID.get();
    }

    /**
     * 设置从记录数
     *
     * @param totalSize
     */
    public static void setTotalSize(Integer totalSize) {
        TOTAL_SIZE.set(totalSize);
    }

    /**
     * 设置监听队列名称
     *
     * @param destination
     */
    public static void setDestination(String destination) {
        SynchronizedOrderDeliverThreadLocal.DESTINATION.set(destination);
    }

    public static String getDestination() {
        return DESTINATION.get();
    }

    /**
     * 获取进度条
     *
     * @return
     */
    public static int getProgress() {
        Integer currentSize = ObjectUtil.defaultIfNull(CURRENT_SIZE.get(), 0);
        Integer totalSize = ObjectUtil.defaultIfNull(TOTAL_SIZE.get(), 0);
        if (totalSize == 0) {
            return 100;
        }
        return NumberUtil.round((double) (currentSize * 100) / totalSize, 0).intValue();
    }

    public static void remove() {
        DESTINATION.remove();
        CURRENT_SIZE.remove();
        TOTAL_SIZE.remove();
        REQUEST_ID.remove();
        IS_WEBSOCKET.remove();
        CREATE_BY.remove();
    }

}
